//
//  AllEquipmVC.m
//  DefendHealth
//
//  Created by 吾诺瀚卓 on 17/3/6.
//  Copyright © 2017年 iMacOne. All rights reserved.
//

#import "AllEquipmVC.h"
#import "AllEquipmCell.h"
#import "EquipmentDetailsVC.h"
@interface AllEquipmVC ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    
    
}

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) UIView *popUpView;                     //底部弹出视图
@end

@implementation AllEquipmVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.TitleNav.text=@"全部设备";
    [self.view addSubview:[self createHeaderView]];
    [self.view addSubview:self.collectionView];
    
    [self.collectionView reloadData];
   
    [self.view addSubview:self.popUpView];
}

//底部弹出视图
-(UIView *)popUpView
{
    if (!_popUpView) {
        
        _popUpView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight-290, kScreenWidth, 290)];
        _popUpView.backgroundColor = K_LineColor;
        _popUpView.hidden = YES;
        
        UIImageView *image = [ReduceCode creatImageVeiwWithFrame:CGRectMake(0, 0, kScreenWidth, 290) imageName:nil isCorner:NO];
        [_popUpView addSubview:image];
        
        UILabel *lab = [ReduceCode creatLabelWithFrame:CGRectMake(0, 5, kScreenWidth, 20) title:@"请从屏幕低端上划打开蓝牙" titleColor:UIColorFromRGB(0x333333) font:14 textAlignment:NSTextAlignmentCenter];
        [image addSubview:lab];
        image.userInteractionEnabled = YES;
        
        UIButton *but = [UIButton creatBtnWithFrame:CGRectMake(kMaxX(image.frame)-40, 5, 30, 30) btnTitle:nil btnTitleColor:nil btnTitleFont:0 btnTagPrice:0 btnSetImage:IMAGE(@"close_small") btnBackgroundColor:nil isCorner:YES action:^(UIButton *button) {
            _popUpView.hidden = YES;
            NSLog(@"<<<<>>>>");
        }];
        [image addSubview:but];
        //imageView 的单击手势
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture)];
        tapGesture.numberOfTapsRequired = 1;
        [image addGestureRecognizer:tapGesture];
    }
    
    return _popUpView;
}
-(void)tapGesture{
    _popUpView.hidden = YES;
}
-(void)leftEvents
{
    //[self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
//初始化collectionView
-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        //调节cell间距
        flowLayout.minimumLineSpacing = 1;
        flowLayout.minimumInteritemSpacing = 1;
        //flowLayout.headerReferenceSize = CGSizeMake(kScreenWidth, 50);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 140+k_NavigationHeight, kScreenWidth, kScreenHeight-k_NavigationHeight-140) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = K_LineColor;
        [_collectionView registerClass:[AllEquipmCell class] forCellWithReuseIdentifier:@"cellId"];
        
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    }
    return _collectionView;
}
//顶部视图
-(UIView *)createHeaderView
{
    self.TitleNav.textColor = UIColorFromRGB(0x888888);
    [self.leftbutton setImage:IMAGE(@"nav_icon_back") forState:UIControlStateNormal];
    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, k_NavigationHeight, kScreenWidth, 140)];
    headerView.backgroundColor = [UIColor whiteColor];
    UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    lineV.backgroundColor = K_LineColor;
    [headerView addSubview:lineV];
    
    CGFloat weight = (kScreenWidth-160)/4;
    NSArray *title = @[@"扫描添加",@"蓝牙配对"];
    for (int i = 0; i<2; i++) {
        UIImageView *imageView = [ReduceCode creatImageVeiwWithFrame:CGRectMake(weight+i*(weight*2+80), 20, 80, 80) imageName:nil isCorner:YES];
        imageView.backgroundColor = K_LineColor;
        [headerView addSubview:imageView];
        
        UIButton *but = [UIButton creatBtnWithFrame:CGRectMake(weight+i*(weight*2+80), kMaxY(imageView.frame)+5, 80, 30) btnTitle:title[i] btnTitleColor:UIColorFromRGB(0xe85f2b) btnTitleFont:15 btnTagPrice:i+10 btnSetImage:nil btnBackgroundColor:nil isCorner:YES action:^(UIButton *button) {
            
            if (button.tag == 11) {
                if (_popUpView.hidden) {
                    _popUpView.hidden = NO;
                }
                else
                {
                    _popUpView.hidden = YES;
                }
            }
        }];
        [headerView addSubview:but];
    }
    
    return headerView;
}
#pragma mark-collectionView的协议方法
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 12;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AllEquipmCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    cell.equipmName.text = @"精灵ELF";
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat cellWidth = (kScreenWidth-3)/3;
    return CGSizeMake(cellWidth, cellWidth+30);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    EquipmentDetailsVC *edVC = [[EquipmentDetailsVC alloc]init];
    edVC.titleName = @"精灵ELF";
    [self presentViewController:edVC animated:YES completion:nil];
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(nonnull NSString *)kind atIndexPath:(nonnull NSIndexPath *)indexPath
{
    UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"forIndexPath:indexPath];
    header.backgroundColor = k_TableBackColor;
    
    UILabel *labelOne = [ReduceCode creatLabelWithFrame:CGRectMake(10, 5, 100, 30) title:@"全部设备" titleColor:UIColorFromRGB(0x333333) font:16 textAlignment:NSTextAlignmentLeft];
    [header addSubview:labelOne];
    UILabel *labelTwo = [ReduceCode creatLabelWithFrame:CGRectMake(kMaxX(header.frame)-150, 5, 140, 30) title:@"点击图片查看详情" titleColor:UIColorFromRGB(0xb2b2b2) font:14 textAlignment:NSTextAlignmentRight];
    [header addSubview:labelTwo];
    return header;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(kScreenWidth, 40);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
